<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 			if (!function_exists('missing_field')) {
				function missing_field($data, $array_required = null) {   
					if (is_array($array_required)) {
						foreach ($array_required as $field) {
							if (trim($data[$field]) == '') {
								return true;
								break;
							}
						}
					}
					
					return false;
	             }
			}
 			
 			/**
              * Verifica se o CNPJ � valido
              * @param     string
              * @return     bool
              */
			if (!function_exists('valid_cnpj')) {
	             function valid_cnpj($str)
	             {
	                 if (strlen($str) <> 18) return FALSE;
	                 $soma1 = ($str[0] * 5) +
	                         ($str[1] * 4) +
	                         ($str[3] * 3) +
	                         ($str[4] * 2) +
	                         ($str[5] * 9) +
	                         ($str[7] * 8) +
	                         ($str[8] * 7) +
	                         ($str[9] * 6) +
	                         ($str[11] * 5) +
	                         ($str[12] * 4) +
	                         ($str[13] * 3) +
	                         ($str[14] * 2);
	                 $resto = $soma1 % 11;
	                 $digito1 = $resto < 2 ? 0 : 11 - $resto;
	                 $soma2 = ($str[0] * 6) +
	                         ($str[1] * 5) +
	                         ($str[3] * 4) +
	                         ($str[4] * 3) +
	                         ($str[5] * 2) +
	                         ($str[7] * 9) +
	                         ($str[8] * 8) +
	                         ($str[9] * 7) +
	                         ($str[11] * 6) +
	                         ($str[12] * 5) +
	                         ($str[13] * 4) +
	                         ($str[14] * 3) +
	                         ($str[16] * 2);
	                 $resto = $soma2 % 11;
	                 $digito2 = $resto < 2 ? 0 : 11 - $resto;
	                 return (($str[16] == $digito1) && ($str[17] ==
	         $digito2));
             	}
			}
            
             /**
              * Verifica se o CPF informado � valido
              * @param     string
              * @return     bool
              */
			if (!function_exists('valid_cpf')) {
				function valid_cpf($cpf) {
	                 // Verifiva se o n�mero digitado cont�m todos os digitos
	                 $cpf = str_pad(ereg_replace('[^0-9]', '', $cpf), 11, '0', STR_PAD_LEFT);
	                
	                 // Verifica se nenhuma das sequ�ncias abaixo foi digitada, caso seja, retorna falso
	                 if (strlen($cpf) != 11 ||
	                     $cpf == '00000000000' ||
	                     $cpf == '11111111111' ||
	                     $cpf == '22222222222' ||
	                     $cpf == '33333333333' ||
	                     $cpf == '44444444444' ||
	                     $cpf == '55555555555' ||
	                     $cpf == '66666666666' ||
	                     $cpf == '77777777777' ||
	                     $cpf == '88888888888' ||
	                     $cpf == '99999999999') {
	                     return FALSE;
	                 } else {
	                     // Calcula os n�meros para verificar se o CPF � verdadeiro
	                     for ($t = 9; $t < 11; $t++) {
	                         for ($d = 0, $c = 0; $c < $t; $c++) {
	                             $d += $cpf{$c} * (($t + 1) - $c);
	                         }
	            
	                         $d = ((10 * $d) % 11) % 10;
	                         if ($cpf{$c} != $d) {
	                             return FALSE;
	                         }
	                     }
	                     return TRUE;
	                 }
	             }
			}
            

			if (!function_exists('valid_date')) {
				function valid_date($date, $format = 'dd/mm/yyyy') {   
	
	                 $dateArray = explode("/", $date); // slice the date to get the day, month and year separately.
	            
	                 $d = 0;
	                 $m = 0;
	                 $y = 0;
	                 if (sizeof($dateArray) == 3) {
	                     if (is_numeric($dateArray[0]))
	                         $d = $dateArray[0];
	                     if (is_numeric($dateArray[1]))
	                         $m = $dateArray[1];
	                     if (is_numeric($dateArray[2]))
	                         $y = $dateArray[2];
	                 }
	            
	                 return checkdate($m, $d, $y) == 1;
	             }
			}
			
			if (!function_exists('format_date')) {
				function format_date($date, $out = false, $show_hour = false, $long_text = false) {   
					if (!trim($date))
					{
						return '';
					}
					
					$date_array = explode(' ', $date);
					$date = $date_array[0];
					if (isset($date_array[1]))
					{
						$hour = $date_array[1];
					}
					
					if (!$out) 
	                {
						$dateArray = explode("/", $date); // slice the date to get the day, month and year separately.
	                	return $dateArray[2] . '-' . str_pad($dateArray[1], 2, '0', STR_PAD_LEFT) . '-' . str_pad($dateArray[0], 2, '0', STR_PAD_LEFT) . (isset($hour) ? $hour : ' 00:00:00');
	                }
	                else
	                {
	                	$dateArray = explode("-", $date); // slice the date to get the day, month and year separately.
	                	return ($long_text ? '%s ' : '') .
                			str_pad($dateArray[2], 2, '0', STR_PAD_LEFT) . '/' . str_pad($dateArray[1], 2, '0', STR_PAD_LEFT) . '/' . $dateArray[0] .
                			($show_hour && isset($hour) ? ($long_text ? ', %s ' : ' ') . substr($hour, 0, 5) : '');
	                }
	             }
			}
			
			
			if (!function_exists('checkbox2db')) {
				function checkbox2db($arrayCheck, &$arrayData) {
					foreach ($arrayCheck as $value) {
						$arrayData[$value] = isset($arrayData[$value]) && $arrayData[$value] == 'Y' ? 'Y' : 'N';
					}
				}
			}
			
			if (!function_exists('debug')) {
				function debug($data) {
					print_r($data);
					exit;
				}
			}
			
				/**
				 * Função simples para validar um link de video do Youtube.
				 *
				 * @author Thiago Bocchile 
				 * @uses cURL
				 * @param string $link Link de video ou compativel que contenha o ID
				 * @return bool
				 */
			if (!function_exists('valid_youtube'))
			{
				function valid_youtube($link)
				{
					 if (preg_match('/(?<=\\?v=)[^&]*/',$link, $matches, PREG_OFFSET_CAPTURE))
					 {
						$ch = curl_init();
						
						curl_setopt($ch, CURLOPT_URL, 'http://youtube.com/v/' . $matches[0][0]);
						curl_setopt($ch, CURLOPT_NOBODY, true);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
											
						$a = curl_exec($ch);
						$i = curl_getinfo($ch);
						
						curl_close($ch);
						
						if ($i["http_code"] != "404")
						{
							return $matches[0][0];
						}
					 }
					 
					 return false;
				}
			}
?>